package com.hongguowei.bigdecimal;

import java.math.BigDecimal;

public class BigDecimalTest {
    public static void main(String[] args) {
        double d1 = 1.111111111111112;
        double d2 = 1.111111111111113;
        double r1 = d1+d2;
        System.out.println("double16位以内的结果"+r1);

        double d3 = 1.1111111111111122;
        double d4 = 1.1111111111111133;
        double r2 = d3+d4;
        System.out.println("double超过16位的结果"+r2);
        BigDecimal bigDecimal1 = BigDecimal.valueOf(d4);
        BigDecimal bigDecimal2 = new BigDecimal(Double.toString(d4));

        BigDecimal b5 = new BigDecimal("1.1111111111111122");
        BigDecimal b6 = new BigDecimal("1.1111111111111133");
        BigDecimal r3 = b5.add(b6);
        System.out.println("BigDecimal超过16位的结果"+r3);

        BigDecimal a = new BigDecimal("3");
        BigDecimal v = new BigDecimal("9");
        //加法
        BigDecimal result = a.add(v);
        System.out.println("加法后的结果为:"+result);
        //减法
        BigDecimal result2 = a.subtract(v);
        System.out.println("减法后的结果为:"+result2);
        //乘法
        BigDecimal result3 = a.multiply(v);
        System.out.println("乘法后的结果为:"+result3);
        //除法
        BigDecimal result4 = a.divide(v,2,BigDecimal.ROUND_UP);
        System.out.println("除法后的结果为:"+result4);
//        比较 1代表a>v -1代表a<v 0代表a=v
        int num = a.compareTo(v);
        int num2 = v.compareTo(a);
        System.out.println(num);
        System.out.println(num2);

        BigDecimal c = new BigDecimal("0.00");
        BigDecimal f = new BigDecimal("0.0");
        BigDecimal h = new BigDecimal(0.00);
        BigDecimal j = new BigDecimal(0.0);
        BigDecimal e =  BigDecimal.ZERO;
        System.out.println("c和e:"+c.equals(e));
        System.out.println("c和f:"+c.equals(f));
        System.out.println("h和j:"+h.equals(j));

        System.out.println(c.compareTo(f)==0);

        //四舍五入
        BigDecimal bigDecimal = new BigDecimal("3.141");
        BigDecimal bigDecimal3 = new BigDecimal("1.156");

        BigDecimal v1 = bigDecimal.setScale(2, BigDecimal.ROUND_UP);
        BigDecimal v11 = bigDecimal3.setScale(2, BigDecimal.ROUND_UP);

        BigDecimal v2 = bigDecimal.setScale(2, BigDecimal.ROUND_DOWN);
        BigDecimal v22 = bigDecimal3.setScale(2, BigDecimal.ROUND_DOWN);

        BigDecimal v3 = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal v33 = bigDecimal3.setScale(2, BigDecimal.ROUND_HALF_UP);

        BigDecimal v4 = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_DOWN);
        BigDecimal v44 = bigDecimal3.setScale(2, BigDecimal.ROUND_HALF_DOWN);

        BigDecimal v5 = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_EVEN);
        BigDecimal v55 = bigDecimal3.setScale(2, BigDecimal.ROUND_HALF_EVEN);

        BigDecimal v6 = bigDecimal.setScale(2, BigDecimal.ROUND_CEILING);
        BigDecimal v7 = bigDecimal.setScale(2, BigDecimal.ROUND_FLOOR);
        System.out.println(bigDecimal);

        System.out.println("bigDecimal四舍五入ROUND_UP的结果为"+v1);
        System.out.println("bigDecimal3四舍五入ROUND_UP的结果为"+v11);

        System.out.println("bigDecimal四舍五入ROUND_DOWN的结果为"+v2);
        System.out.println("bigDecimal3四舍五入ROUND_DOWN的结果为" + v22);

        System.out.println("bigDecimal四舍五入ROUND_HALF_UP的结果为"+v3);
        System.out.println("bigDecimal3四舍五入ROUND_HALF_UP的结果为"+v33);

        System.out.println("bigDecimal四舍五入ROUND_HALF_DOWN的结果为"+v4);
        System.out.println("bigDecimal3四舍五入ROUND_HALF_DOWN的结果为"+v44);

        System.out.println("bigDecimal四舍五入ROUND_HALF_EVEN的结果为"+v5);
        System.out.println("bigDecimal3四舍五入ROUND_HALF_EVEN的结果为"+v55);

        System.out.println("四舍五入ROUND_CEILING的结果为"+v6);
        System.out.println("四舍五入ROUND_FLOOR的结果为"+v7);

    }
}
